using System;
using Proto3Msg;
using UnityEngine;

public class MoveServer
{
    private ServerNetManager _netMgr = new ServerNetManager();

    public MoveServer()
    {
        MsgDictDefine.Reg(
                "Proto3Msg.MsgMove",
                "Proto3Msg.MsgPing"
                );

        var packer = new Proto3MsgPacker();
        packer.MsgDefine = MsgDictDefine.Instance;
        _netMgr.Packer = packer;
        _netMgr.AddMsgHandler(this);
        _netMgr.Start(8888);
    }

    public void Update()
    {
        _netMgr.Loop();
    }


    public void Close()
    {
        _netMgr.Close();
    }
//=========================================================================================

    public void MSG_OnConnect(object msg,ClientState client)
    {
        Debug.Log("Connect:"+client.Sock.RemoteEndPoint);
    }

    public void MSG_OnDisconnect(object msg,ClientState client)
    {
        Debug.Log("Disconnect:"+client.Sock.RemoteEndPoint);
    }

    public void MSG_OnTimer(object msg,ClientState client){

    }

    public void MSG_OnError(object e,ClientState client)
    {
        Debug.Log(e.ToString());
    }

    public void MSG_MsgMove(object msg,ClientState client)
    {
        var move = msg as MsgMove;
        var str = String.Format("move:[{0},{1},{2}]", move.X.ToString(), move.Y.ToString(), move.Z.ToString());
        Debug.Log(str);
        move.X += 1;
        _netMgr.Send(client,move);
    }

    public void MSG_MsgPing(object msg,ClientState client)
    {
        _netMgr.Send(client,msg);
    }
}
